package edu.haiu.training.ui;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import edu.haiu.training.data.User;
import edu.haiu.training.data.UserState;

public class TagAction {
	private static final String username1 ="root";
	private static final String password1 ="123456";
	private static final String database1 ="jdbc:mysql://localhost:3306/mydb";
	private String name;
	private String actionType;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	 public String execute3(){
	     System.out.println("==========================================");
	     System.out.println("(int)((0.1+0.7)*10) =" + (int)((0.1+0.7)*10));
	     System.out.println("==========================================");
		 return "success";
	 }

		List<User>users1 = new ArrayList<User>();
		
		public List<User> getUsers() {
			return users1;
		}
		public void setUsers(List<User> users1) {
			this.users1 = users1;
		}
	 	public String execute() throws Exception{

			Connection connection = DriverManager.getConnection(database1,username1,password1);
			String qs= "select * from mydb.user";
			PreparedStatement st= connection.prepareStatement(qs);
			ResultSet rs = st.executeQuery(qs);
			while(rs.next()){
				 User user = new User();
				 user.setId1(rs.getInt("Id"));
				 user.setUsername(rs.getString("Username"));
				 user.setPassword(rs.getString("Password"));
				 user.setFirstName(rs.getString("FirstName"));
				 user.setLastName(rs.getString("LastName"));
				 user.setAddress(rs.getString("Address"));
				 user.setBirthday(String.valueOf(new java.sql.Date(rs.getDate("Birthday").getTime())));
				 user.setAdministrator(rs.getBoolean("Administrator"));		 
				 user.setState(UserState.values()[rs.getInt("State")]);
				 users1.add(user);
			}
	 		String result = "";
	 		if(actionType.equals("userManagement"))
	 			result = "users";
	 		else if(actionType.equals("contentManagement"))
	 			result = "content";
	 		return result;
	 	}
	 	public String execute1() throws Exception{
	 		String result = "";
	 		if(actionType.equals("add"))
	 			result = "add";
	 		else if(actionType.equals("edit"))
	 			result = "edit";
	 		else if(actionType.equals("delete"))
	 			result = "delete";
	 		return result;
	 	}
	 	
	 	public String getActionType() {
	 		return actionType;
	 	}

	 	public void setActionType(String actionType) {
	 		this.actionType = actionType;
	 	}
	 }

